﻿/// <reference path="../novas/nova.all.js" />

var BloggerDbContext = function () {
    nova.data.DbContext.call(this, "BloggerDB", "1.2", "BloggerDB", 1000000);
    this.logSqls = true;
    this.alertErrors = true;
    this.blogs = new nova.data.Repository(this, Blog, "blogs");
    this.settings = new nova.data.Repository(this, Setting, "settings");
};

BloggerDbContext.prototype = new nova.data.DbContext();
BloggerDbContext.constructor = BloggerDbContext;

BloggerDbContext.prototype.initData = function (callback) {
    var obj = this;
    var urlSetting = new Setting();
    urlSetting.key = Setting.keys.WebServiceUrl;
    urlSetting.value = "http://blogger.leo/upload";
    obj.settings.add(urlSetting);

    var intervalSetting = new Setting();
    intervalSetting.key = Setting.keys.AutoSaveIntervalSeconds;
    intervalSetting.value = 5;
    obj.settings.add(intervalSetting);

    obj.saveChanges(function () {
        nova.data.DbContext.prototype.initData.call(callback);
    }, null);
};

var Blog = function () {
    nova.data.Entity.call(this);
    this.title = "";
    this.content = "";
    this.createdDate = new Date();
    this.lastUpdatedDate = new Date();
};

Blog.prototype = new nova.data.Entity();
Blog.constructor = Blog;

Blog.prototype.update = function (blog) {
    this.title = blog.title;
    this.content = blog.content;
    this.lastUpdatedDate = new Date();
};

var Setting = function () {
    nova.data.Entity.call(this);
    this.key = "";
    this.value = "";
};

Setting.prototype = new nova.data.Entity();
Setting.constructor = Setting;

Setting.keys = {
    WebServiceUrl: "WebServiceUrl",
    AutoSaveIntervalSeconds: "AutoSaveIntervalSeconds"
};